package menus;

import javax.faces.event.ValueChangeEvent;
import user.Role;

/**
 *
 * @author Thorsten Ludwig
 */
public class MenuItemContainer {
    
    private LectureMenuItem item;
    
    private MenuCategory parent;

    public MenuItemContainer(LectureMenuItem item) {
        this.item = item;
    }
    
    public MenuItemContainer(LectureMenuItem item, MenuCategory parent) {
        this.item = item;
        this.parent = parent;
    }
    
    public LectureMenuItem getItem() {
        return item;
    }

    public void setItem(LectureMenuItem item) {
        this.item = item;
    }
    
    public void CategoryChanged (ValueChangeEvent event) {
        System.out.println("Category gewechselt");
        MenuCategory oldCat = (MenuCategory) event.getOldValue();
        MenuCategory newCat = (MenuCategory) event.getNewValue();
        if (oldCat != null) {
            System.out.println("Entferne von " + oldCat.getName() + " Item namens " + this.item.getName());
            oldCat.removeItem(this.item);
        }
        if (newCat != null) {
            System.out.println("Füge zu " + newCat.getName() + " Item namens " + this.item.getName() + "hinzu");
            newCat.addItem(this.item);
        }
            
    }

    public MenuCategory getParent() {
        return parent;
    }

    public void setParent(MenuCategory parent) {
        System.out.println("Setze neuen Parent von " + this.item.getName());        
        if (parent != null)
            System.out.println("Parent ist " + parent.getName());
        else
            System.out.println("Parent ist null");
        this.parent = parent;
    }
    
    public void setAdminViewRights(boolean value) {
    }
    
    public void setCoAdminViewRights(boolean value) {
    }
    
    public void setTutorViewRights(boolean value) {
    }
    
    public void setStudentViewRights(boolean value) {
    }
    
    public void setGuestViewRights(boolean value) {
    }
    
    public boolean getAdminViewRights() {
        return this.item.getRights().hasViewRight(Role.ADMIN);
    }
    
    public boolean getCoAdminViewRights() {
        return this.item.getRights().hasViewRight(Role.COADMIN);
    }
    
    public boolean getTutorViewRights() {
        return this.item.getRights().hasViewRight(Role.TUTOR);
    }
    public boolean getStudentViewRights() {
        return this.item.getRights().hasViewRight(Role.STUDENT);
    }
    
    public boolean getGuestViewRights() {
        return this.item.getRights().hasViewRight(Role.GUEST);
    }
    
    public void adminViewListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveViewRights(Role.ADMIN);
        else
            this.item.getRights().removeViewRights(Role.ADMIN);
    }
    
    public void coadminViewListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveViewRights(Role.COADMIN);
        else
            this.item.getRights().removeViewRights(Role.COADMIN);
    }
    
    public void tutorViewListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveViewRights(Role.TUTOR);
        else
            this.item.getRights().removeViewRights(Role.TUTOR);
    }
    
    public void studentViewListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveViewRights(Role.STUDENT);
        else
            this.item.getRights().removeViewRights(Role.STUDENT);
    }
    
    public void guestViewListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveViewRights(Role.GUEST);
        else
            this.item.getRights().removeViewRights(Role.GUEST);
    }
    
    
    public boolean getAdminEditRights() {
        return this.item.getRights().hasEditRight(Role.ADMIN);
    }
    
    public boolean getCoAdminEditRights() {
        return this.item.getRights().hasEditRight(Role.COADMIN);
    }
    
    public boolean getTutorEditRights() {
        return this.item.getRights().hasEditRight(Role.TUTOR);
    }
    public boolean getStudentEditRights() {
        return this.item.getRights().hasEditRight(Role.STUDENT);
    }
    
    public boolean getGuestEditRights() {
        return this.item.getRights().hasEditRight(Role.GUEST);
    }
    
    public void setAdminEditRights(boolean value) {
    }
    
    public void setCoAdminEditRights(boolean value) {
    }
    
    public void setTutorEditRights(boolean value) {
    }
    
    public void setStudentEditRights(boolean value) {
    }
    
    public void setGuestEditRights(boolean value) {
    }
    
    public void adminEditListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveEditRights(Role.ADMIN);
        else
            this.item.getRights().removeEditRights(Role.ADMIN);
    }
    
    public void coadminEditListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveEditRights(Role.COADMIN);
        else
            this.item.getRights().removeEditRights(Role.COADMIN);
    }
    
    public void tutorEditListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveEditRights(Role.TUTOR);
        else
            this.item.getRights().removeEditRights(Role.TUTOR);
    }
    
    public void studentEditListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveEditRights(Role.STUDENT);
        else
            this.item.getRights().removeEditRights(Role.STUDENT);
    }
    
    public void guestEditListener(ValueChangeEvent event) {
        boolean wert = (Boolean) event.getNewValue();
        System.out.println("Ändere Wert von " + this.item.getName() + " auf " + wert);
        if (wert)
            this.item.getRights().giveEditRights(Role.GUEST);
        else
            this.item.getRights().removeEditRights(Role.GUEST);
    }
}